package com.nervenets.shopping.controller.order.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.enumeration.OrderStatus;
import com.nervenets.shopping.hibernate.domain.order.GroupBuyOrderUser;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class GroupBuyOrderUserPagingParams extends EntityPagingParams<GroupBuyOrderUser> {
    @ApiModelProperty(value = "商品ID，不传不筛选")
    private long productId;
    @ApiModelProperty(value = "用户ID，不传不筛选")
    private long userId;
    @ApiModelProperty(value = "订单状态，不传不筛选")
    private List<OrderStatus> statuses;
    @ApiModelProperty(value = "下单开始时间，不传不筛选")
    private long startTime;
    @ApiModelProperty(value = "下单结束时间，不传不筛选")
    private long endTime;
    @ApiModelProperty(value = "客户是否确认通知：0全部、1是、2否")
    private int notify;
    @ApiModelProperty(value = "关键词，不传不筛选")
    private String keyword;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<GroupBuyOrderUser> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("product").get("id"), productId, productId > 0);
        addEqualsPredicate(root.get("createBy"), userId, userId > 0);
        addInPredicate(root.get("status"), statuses);
        addBetweenPredicate(root.get("createTime"), startTime, endTime);
        addYesOrNoPredicate(root.get("notify"), notify, 1, 2);
        addOrLikePredicate(keyword, root.get("name"), root.get("mobile"), root.get("address"), root.get("expressNumber"), root.get("outTradeNo"));
    }
}
